package com.wzcl.app.model.vo;

import com.wzcl.app.dao.model.AppUser;
import com.wzcl.app.dao.model.CourseComment;
import com.wzcl.app.service.impl.UserServiceImpl;
import com.wzcl.app.utils.SpringUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class CourseCommentVo extends BaseVo{

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "课程Id")
    private Integer courseId;

    @ApiModelProperty(value = "用户Id")
    private Integer userId;

    @ApiModelProperty(value = "用户顶电话")
    private String userPhone;

    @ApiModelProperty(value = "用户nickName")
    private String userNickName;

    @ApiModelProperty(value = "头像")
    private String userHeadIcon;

    @ApiModelProperty(value = "评论")
    private String comment;

    @ApiModelProperty(value = "父评论ID")
    private Integer parentId;

    @ApiModelProperty(value = "子评论")
    List<CourseCommentVo> commentList;

    private CourseCommentVo(){

    }

    public static CourseCommentVo create(CourseComment comment){
        CourseCommentVo courseCommentVo = new CourseCommentVo();
        BeanUtils.copyProperties(comment,courseCommentVo);
        if(comment.getCommentList()!=null && comment.getCommentList().size()>0){
            List<CourseCommentVo> collect = comment.getCommentList().stream().map(CourseCommentVo::create).collect(Collectors.toList());
            courseCommentVo.setCommentList(collect);
        }
        courseCommentVo.translationValueStr();
        courseCommentVo.processResources();
        return courseCommentVo;
    }


    @Override
    public void translationValueStr() {

    }

    @Override
    public void processResources() {
        try {
            UserServiceImpl userService = (UserServiceImpl) SpringUtil.getBean("userServiceImpl");
            AppUser user = userService.getUserById(this.userId);
            this.userNickName = user.getNickName();
            this.userHeadIcon = user.getHeadIcon()==null?DEFAULT_AVATAR_IMAGE:user.getHeadIcon();
        }catch (Exception e){

        }
    }

    @Override
    public String toString() {
        return "CourseCommentVo{" +
                "courseId=" + courseId +
                ", userId=" + userId +
                ", userPhone='" + userPhone + '\'' +
                ", userNickName='" + userNickName + '\'' +
                ", userHeadIcon='" + userHeadIcon + '\'' +
                ", comment='" + comment + '\'' +
                ", parentId=" + parentId +
                ", commentList=" + commentList +
                '}';
    }
}
